package org.exmaple;

/**
 * 修饰一个类
 *给class加锁和上例的给静态方法加锁是一样的，所有对象公用一把锁
 */
class Test5 implements Runnable {
    private static int count;

    public void SyncThread() {
        count = 0;
    }

    public static void method() {
        synchronized(SyncThread.class) {
            for (int i = 0; i < 5; i ++) {
                try {
                    System.out.println(Thread.currentThread().getName() + ":" + (count++));
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void run() {
        method();
    }
}
